/* Multi-dimensional C array allocation */

/*
 * Copyright (c) 2002, Jean-Sebastien Roy (js@jeannot.org)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/* $Id: mdmalloc.h,v 1.1 2006/06/27 15:35:05 bruhns Exp $ */

#ifndef _MDMALLOC_
#define _MDMALLOC_

#include <stddef.h>

#if defined(__cplusplus)
extern "C" {
#endif

  /*
   * Allocate a multi-dimensional array
   * returns and allocated continuous zone accessible like a multi-dimensionnal
   * array.
   * el_size : array element size
   * n : # of dimensions
   * ... : n dimensions
   *
   * Example:
   * 	double **array;
   * 	array = vmdmalloc(sizeof(double), 2, 10, 20);
   * 	array[5][17] = 3.0;
   * 	free(array);
   */
  extern void* vmdmalloc(size_t el_size, size_t  n, ...);

  /*
   * Allocate a multi-dimensional array
   * (Same as vmdmalloc, without variable arguments)
   * el_size : array element size
   * n : # of dimensions
   * d : array of n dimensions
   */
  extern void* mdmalloc(size_t el_size, size_t  n, size_t* d);

  /*
   * Return a pointer to the first element of a multi-dimensional array
   * array : pointer to multi-dimensional array
   * n : # of dimensions of array
   */
  extern void* mdfirst(void* array, size_t  n);

  /*
   * Copy the contents of a multi-dimensional array to another one
   * dest : already allocated multi-dimensional array
   * src : multi-dimensional array to copy
   * el_size : array element size
   * n : # of dimensions
   * ... : the n dimensions
   * src and dest must have the same dimensions.
   */
  extern void vmdcopy(void* dest, void* src, size_t el_size, size_t n, ...);

  /*
   * Copy the contents of a multi-dimensional array to another one
   * (same as vmdcopy, without variable arguments)
   * dest : already allocated multi-dimensional array
   * src : multi-dimensional array to copy
   * el_size : array element size
   * n : # of dimensions
   * d : array of n dimensions
   * src and dest must have the same dimensions.
   */
  extern void mdcopy(void* dest, void* src, size_t el_size, size_t  n, size_t* d);

#if defined(__cplusplus)
}
#endif

#endif /* _MDMALLOC_ */
